//
//  MAPYLocationManager.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/17.
//

import UIKit
import CoreLocation

let mapy_locNoti = Notification.Name("mapy_locNoti")

extension MAPYManager {
    func mapy_isCanLocation(mapy_complete:((Bool)->())? = nil) {
        if #available(iOS 14.0, *) {
            DispatchQueue.global().async {
                if (self.mapy_loctionManager.authorizationStatus == .notDetermined || self.mapy_loctionManager.authorizationStatus == .authorizedWhenInUse) && CLLocationManager.locationServicesEnabled() {
                    DispatchQueue.main.async {
                        mapy_complete?(true)
                    }
                    
                    return
                }
                if self.mapy_loctionManager.authorizationStatus == .denied {
                    DispatchQueue.main.async {
                        mapy_complete?(false)
                    }
                    
                    return
                }
                DispatchQueue.main.async {
                    mapy_complete?(false)
                }
                
            }
        }else {
            let mapy_state = CLLocationManager.authorizationStatus()
            DispatchQueue.global().async {
                if (mapy_state == .notDetermined || mapy_state == .authorizedWhenInUse) && CLLocationManager.locationServicesEnabled() {
                    DispatchQueue.main.async {
                        mapy_complete?(true)
                    }
                    
                    return
                }
                if mapy_state == .denied {
                    DispatchQueue.main.async {
                        mapy_complete?(false)
                    }
                    
                    return
                }
                DispatchQueue.main.async {
                    mapy_complete?(false)
                }
                
            }
        }
        
    }
    func mapy_requesAuthLocation() {
        mapy_loctionManager.requestWhenInUseAuthorization()
    }
    func mapy_requesLocation() {
        self.mapy_isCanLocation(mapy_complete: { mapy_state in
            if !mapy_state {
                return
            }
            self.mapy_loctionManager.requestLocation()
        })
        
    }
    //MARK: - mapy 请求定位权限
    func mapy_configLocManager() {
        mapy_loctionManager.distanceFilter = 3000
        mapy_loctionManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
        mapy_loctionManager.delegate = self
        mapy_loctionManager.allowsBackgroundLocationUpdates = false
        mapy_loctionManager.requestWhenInUseAuthorization()
    }
}
//MARK: - mapy CLLocationManagerDelegate
extension MAPYManager: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        if #available(iOS 14.0, *) {
            if manager.authorizationStatus == .authorizedWhenInUse {
                mapy_requesLocation()
                NotificationCenter.default.post(name: mapy_locNoti, object: nil)
            }
        } else {
            if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
                mapy_requesLocation()
                NotificationCenter.default.post(name: mapy_locNoti, object: nil)
            }// Fallback on earlier versions
        }
    }
    
    func mapy_requestLocationAlertAuth() {
        mapy_isCanLocation { mapy_state in
            if  !mapy_state {
                MAPYCommon.mapy_alertShow(mapy_title: "Location", mapy_message: "Request your location permission to obtain the speed information of your current network")
            }
        }
    }
}
